<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\FormBuilder;
use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageServiceComment;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\State;
use support\Request;
use think\facade\Db;

class ServiceCommentController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->addScreen('content', 'input', '内容', [
            'placeholder' => '内容搜索'
        ]);
        $builder->addScreen('reply', 'input', '回复', [
            'placeholder' => '回复搜索'
        ]);
        $builder->addScreen('examine', 'select', '审核', [
            'options' => Examine::getOptions()
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addRightButton('examine', '审核', [
            'api'    => $this->pluginPrefix . '/admin/ServiceComment/examine',
            'path' => '/ServiceComment/examine',
            'type'      => 'modal',
            'params' => [
                'field' => 'examine',
                'where' => '=',
                'value' => Examine::WAIT['value']
            ]
        ], [], [
            'type' => 'danger',
            'link' => true
        ]);
        $builder->addRightButton('del', '删除', [
            'type'   => 'confirm',
            'api'    => $this->pluginPrefix . '/admin/ServiceComment/delete',
            'method' => 'delete'
        ], [
            'title'   => '温馨提示',
            'content' => '是否确认删除该数据',
        ], [
            'type' => 'error',
            'link' => true
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称', [
                'width' => 120,
            ])
            ->addColumnEle('thumb', '封面', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('title', '服务名称', [
                'width' => 150,
            ])
            ->addColumn('content', '内容', [])
            ->addColumnEle('images', '图片', [
                'width' => 200,
                'params' => [
                    'type' => 'images',
                ],
            ])
            ->addColumn('reply', '回复', [])
            ->addColumn('grade', '评分', [
                'width' => 100
            ])
            ->addColumnEle('anony', '匿名', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => [
                        0 => '否',
                        1 => '是'
                    ],
                    'style' => [
                        0 => [
                            'type' => 'info'
                        ],
                        0 => [
                            'type' => 'danger'
                        ]
                    ],
                ]
            ])
            ->addColumnEle('examine', '审核', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => Examine::dictOptions(),
                    'style' => Examine::dictOptions('style')
                ],
            ])
            ->addColumnEle('show', '显示', [
                'width' => 100,
                'params' => [
                    'type' => 'switch',
                    'api' => $this->pluginPrefix . '/admin/ServiceComment/updateState',
                    'checked' => State::YES,
                    'unchecked' => State::NO
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['c.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if ($uid) {
            $where[] = ['c.uid', '=', $uid];
        }
        $content = $request->get('content');
        if ($content) {
            $where[] = ['c.content', 'like', "%{$content}%"];
        }
        $reply = $request->get('reply');
        if ($reply) {
            $where[] = ['c.reply', 'like', "%{$reply}%"];
        }
        $examine = $request->get('examine');
        if ($examine !== null) {
            $where[] = ['c.examine', '=', $examine];
        }
        $data = MassageServiceComment::where($where)
            ->alias('c')
            ->join('MassageService s', 's.id = c.service_id')
            ->join('MassageUser u', 'u.id = c.uid')
            ->order('c.create_time desc')
            ->field('c.*,u.nickname,u.headimg,s.thumb,s.title')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->nickname = base64_decode($item->nickname);
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
                $item->thumb = UploadService::url($item->thumb);
            });
        return $this->successRes($data);
    }
    public function examine(Request $request)
    {
        $id = $request->get('id');
        $MassageServiceComment = MassageServiceComment::where(['id' => $id, 'saas_appid' => $request->saas_appid, 'examine' => Examine::WAIT['value']])->find();
        if (!$MassageServiceComment) {
            return $this->fail('数据不存在');
        }
        if ($request->method() === 'POST') {
            $D = $request->post();
            Db::startTrans();
            try {
                $MassageServiceComment->examine = $D['examine'];
                $MassageServiceComment->save();
                Db::commit();
                return $this->success('审核成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('examine', 'radio', '状态', Examine::YES['value'], [
            'col' => 24,
            'options' => Examine::getOptions()
        ]);
        $data = $builder->setData($MassageServiceComment)->create();
        return $this->successRes($data);
    }
    public function updateState(Request $request)
    {
        $id = $request->post('id');
        $MassageServiceComment = MassageServiceComment::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        $MassageServiceComment->show = $MassageServiceComment->show == State::YES['value'] ? State::NO['value'] : State::YES['value'];
        if ($MassageServiceComment->save()) {
            return $this->success('修改成功');
        }
        return $this->fail('修改失败');
    }
    public function delete(Request $request)
    {
        $id = $request->post('id');
        $where = [
            ['saas_appid', '=', $request->saas_appid],
            ['id', '=', $id]
        ];
        $MassageServiceComment = MassageServiceComment::where($where)->find();
        if (!$MassageServiceComment) {
            return $this->fail('数据不存在');
        }
        if ($MassageServiceComment->delete()) {
            return $this->success('删除成功');
        }
        return $this->fail('删除失败');
    }
}
